package com.nw.serviceImpl;

import com.nw.dao.CargoDao;
import com.nw.entity.Cargo;
import com.nw.service.CargoService;
import com.nw.util.AliyunOSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CargoServiceImpl implements CargoService {
    @Resource
    CargoDao cargoDao;

    @Override
    public List<Cargo> queryAllCargo() {
        List<Cargo> cargos = cargoDao.queryAllCargo();
        return cargos;
    }

    @Override
    public HashMap<String, Object> addCargo(Cargo cargo) {
        HashMap<String, Object> map = new HashMap();
        try {

            String cargoId = UUID.randomUUID().toString();
            cargo.setCargoId(cargoId);
            cargo.setCargoSaled(0);
            cargo.setParentId(cargo.getParentId());
            System.out.println("cargo数据为:"+cargo);
            cargoDao.addCargo(cargo);
            map.put("code","0");
            map.put("msg","添加商品成功");
            map.put("data",cargoId);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","添加商品失败");
        }
        return map;
    }

    @Override
    public HashMap< String,Object> queryCargoById(String cargoId) {
        Cargo cargo = null;
        HashMap<String, Object> map = new HashMap();

            cargo = cargoDao.queryCargoById(cargoId);
            map.put("code","0");
            map.put("msg","查询成功");
            map.put("data",cargo);
            if (cargo == null) {
                map.put("code","1");
                map.put("msg", "查无此数据");
            }
        return map;
    }

    @Override
    public HashMap<String, Object> queryCargoByCategory(String categoryName) {
        HashMap<String, Object> map = new HashMap<>();
        Cargo cargo = cargoDao.queryCargoByCategory(categoryName);
        map.put("code","0");
        map.put("msg","查询成功");
        map.put("data",cargo);
        if (cargo == null) {
            map.put("code","1");
            map.put("msg", "查无此数据");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> uploadCargoPic(MultipartFile cargoPic, String cargoId) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            //获取文件名
            String originalFilename = cargoPic.getOriginalFilename();
            String newFilename = System.currentTimeMillis()+"-"+originalFilename;
            String objectName = "cargo/"+newFilename;
            AliyunOSSUtil.uploadByteFileAliyun("rwproject",objectName,cargoPic);

            //修改文件信息
            Cargo cargo = new Cargo();
            cargo.setCargoId(cargoId);
            System.out.println("数据库headImg对应oss路径:https://rwproject.oss-cn-beijing.aliyuncs.com/"+objectName);
            cargo.setCargoPic("https://rwproject.oss-cn-beijing.aliyuncs.com/"+objectName);
            cargoDao.uploadCargoPic(cargo);
            map.put("code","0");
            map.put("msg","图片上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","图片上传失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteCargo(Cargo cargo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            Cargo cargo1 = cargoDao.queryCargoById(cargo.getCargoId());
            String[] split = cargo1.getCargoPic().split("/");
            String objectName = split[3] + "/" +split[4];
            AliyunOSSUtil.deleteFileByAliyun("rwproject",objectName);
            cargoDao.deleteCargo(cargo);
            map.put("code","0");
            map.put("msg","商品删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","商品删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateCargoPrice(Cargo cargo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            cargoDao.updateCargoPrice(cargo);
            map.put("code","0");
            map.put("msg","商品删修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
            map.put("msg","商品修改失败");
        }
        return map;
    }
}
